
package com.netease.xone.fragment;

import protocol.XoneCallBack;
import protocol.XoneService;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ExpandableListView;
import android.widget.Toast;

import com.netease.pkgRelated.XoneConsts;
import com.netease.xone.R;
import com.netease.xone.adapter.ExpandSubjectAdapter;

public class FragmentSubscribeSubjectList extends FragmentBase {

    /**
     * 订阅，可展开主题列表（游戏、综合类）
     *
     * @param type
     * @see XoneConsts.SUBJECT_LIST_TYPE
     * @return
     */
    public static FragmentSubscribeSubjectList newInstance(int type) {
        FragmentSubscribeSubjectList fragment = new FragmentSubscribeSubjectList();
        Bundle b = new Bundle();
        b.putInt(XoneConsts.BUNDLE_KEY.SUBSCRIBE_SUBJECT_TYPE, type);
        fragment.setArguments(b);
        return fragment;
    }

    private ExpandSubjectAdapter mAdapter;
    private int mType;
    private int mTid;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mType = getArguments().getInt(XoneConsts.BUNDLE_KEY.SUBSCRIBE_SUBJECT_TYPE, 1);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.fragment_subscribe_subject_list, null);
        ExpandableListView list = (ExpandableListView)v;
        // --计算展开指示器的位置
        final int screenWidth = getActivity().getResources().getDisplayMetrics().widthPixels;
        Drawable indicator = getActivity().getResources().getDrawable(
                R.drawable.selector_subject_expand_indicator);
        final int indicatorWidth = indicator.getIntrinsicWidth();
        final int rightMargin = getActivity().getResources().getDimensionPixelSize(
                R.dimen.subject_item_group_indicator_right);
        int left = screenWidth - rightMargin - indicatorWidth;
        list.setIndicatorBounds(left, left + indicatorWidth);
        // --计算展开指示器的位置end
        mAdapter = new ExpandSubjectAdapter(getActivity());
        XoneService.getInstance().addListener(mCallBack);
        mTid = XoneService.getInstance().doGetSubjectList(mType);
        list.setAdapter(mAdapter);
        return v;
    }

    @Override
    public void onDestroyView() {
        XoneService.getInstance().removeListener(mCallBack);
        mAdapter.removeListener();
        super.onDestroyView();
    };


    XoneCallBack mCallBack = new XoneCallBack() {

        @Override
        public void onGetSubjectList(int transactionId,
                java.util.List<protocol.meta.Subject> list) {
            if (mTid == transactionId) {
                mAdapter.addAll(list);
            }
        };

        @Override
        public void onGetSubjectListError(int transactionId, int errCode, String err) {
            if (mTid == transactionId) {
                Toast.makeText(getActivity(), err, Toast.LENGTH_SHORT).show();
            }
        };
    };
}
